package com.bistu.xiong.tulingrobotchat.activities;


import android.content.Context;
import android.content.Intent;
import android.support.annotation.StringRes;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.EditText;
import android.widget.ImageView;

import com.bistu.xiong.common.app.Activity;
import com.bistu.xiong.common.widget.recycler.RecyclerAdapter;
import com.bistu.xiong.factory.bean.ChatMessage;
import com.bistu.xiong.factory.data.DataSource;
import com.bistu.xiong.factory.data.helper.TuLingHelper;
import com.bistu.xiong.factory.model.api.tuling.TuLingSendModel;
import com.bistu.xiong.factory.persistence.Account;
import com.bistu.xiong.tulingrobotchat.R;
import com.bistu.xiong.tulingrobotchat.adapter.ChatMessageRecAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TuLingActivity extends Activity implements DataSource.Callback<ChatMessage>, RecyclerAdapter.AdapterListener{

    @BindView(R.id.iv_send_msg)
    ImageView ivSendMsg;

    @BindView(R.id.re_message)
    RecyclerView reMessage;

    @BindView(R.id.et_msg)
    EditText edMsg;

    private ChatMessageRecAdapter msgAdapter;
    private List<ChatMessage> mDataList;

    /**
     * TuLingActivity 显示的入口
     *
     * @param context 上下文
     */
    public static void show(Context context) {
        context.startActivity(new Intent(context, TuLingActivity.class));
    }

    @Override
    protected int getCountentLayoutId() {
        return R.layout.activity_tuling_chat;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
    }

    @Override
    protected void initData() {
        super.initData();
        mDataList = new ArrayList<>();
        mDataList.add(new ChatMessage("您好请问有什么需要帮助的么",true));
        msgAdapter = new ChatMessageRecAdapter(mDataList,this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        reMessage.setLayoutManager(linearLayoutManager);
        reMessage.setAdapter(msgAdapter);
    }

    @OnClick(R.id.iv_send_msg)
    void sendMessage(){
        String message = edMsg.getText().toString();
        if (message!=null&&(message.equals(""))){
            toast("发送内容不能为空！");
            return;
        }
        edMsg.setText("");
        msgAdapter.add(new ChatMessage(message));
        reMessage.scrollToPosition(msgAdapter.getItemCount()-1);
        TuLingHelper.sendMsgToTuLing(new TuLingSendModel(message, Account.getUserId()),this);

    }
    @OnClick(R.id.close)
    void close(){
        finish();
    }

    //API 成功回调
    @Override
    public void onDataLoaded(ChatMessage chatMessage) {
        msgAdapter.add(chatMessage);
        reMessage.scrollToPosition(msgAdapter.getItemCount()-1);
    }

    @Override
    public void onDataNotAvailable(@StringRes int strRes) {
        toast("网络错误");
    }

    @Override
    public void onItemClick(RecyclerAdapter.ViewHolder holder, Object o) {

    }

    @Override
    public void onItemLongClick(RecyclerAdapter.ViewHolder holder, Object o) {

    }
}
